<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <el-row>
        <el-col :span="6">
          <el-button type="primary" @click="showAddCate">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 树形表格区域 -->
      <tree-table class="treeTable" :data="goodsCateList" :columns="columns" show-index index-text="#" border :selection-type= "false" :expand-type=" false">
        <!-- 是否有效区域模板 -->
        <template v-slot:effect="slotProps">
          <i class="el-icon-success" v-if="slotProps.row.cat_deleted === false"></i>
          <i class="el-icon-error" v-else></i>
          <!-- {{ slotProps.row }} -->
        </template>
        <!-- 需要权限模板区域 -->
        <template v-slot:rights="slotProps">
          <el-tag size="mini" v-if="slotProps.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" type="success" v-else-if="slotProps.row.cat_level === 1">二级</el-tag>
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作模板区域 -->
        <template v-slot:operate="">
          <el-button type="primary" class="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" class="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页功能组件渲染区域 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="getGoodsCate.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="getGoodsCate.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
    <!-- 添加商品分类会话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%" @close="addCateDialogVisibleClosed">
      <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px" >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <!-- 级联选择器区域 -->
        <el-form-item label="父级分类" label-width="100px">
          <el-cascader
          v-model="selectKeys"
          :options="parentCateList"
          :props="cascaderProps"
          @change="selectCateKeys"
          clearable change-on-select>
          </el-cascader>
        </el-form-item>
      </el-form >
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCateList">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

export default {
  data () {
    return {
      getGoodsCate: { // 获取商品分类列表需要的数据对象
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      goodsCateList: [], // 商品分类列表
      columns: [ // 树形表格需要的数据
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'effect'
        },
        {
          label: '权限',
          type: 'template',
          template: 'rights'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operate'
        }
      ],
      total: 0, // 商品分类数据总条数
      addCateDialogVisible: false, // 控制添加分类会话框显示和隐藏
      addCateForm: { // 添加分类表单数据对象
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addCateFormRules: { // 添加分类表单数据验证对象
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      parentCateList: [], // 父级分类列表
      selectKeys: [], // 级联选择器选中的 id数据
      cascaderProps: { // 级联选择器的选项数据
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
        expandTrigger: 'hover'
      }
    }
  },
  created () {
    this.getGoodsCateList()
  },
  methods: {
    async getGoodsCateList () { // 获取全部商品分类列表
      const { data: res } = await this.$http.get('categories', { params: this.getGoodsCate })
      if (res.meta.status !== 200) return this.$msg.error('获取数据失败！')
      this.goodsCateList = res.data.result
      this.total = res.data.total
    },
    handleSizeChange (newVal) { // 每页显示多少条数据
      this.getGoodsCate.pagesize = newVal
      this.getGoodsCateList()
    },
    handleCurrentChange (newVal) { // 当前页数据
      this.getGoodsCate.pagenum = newVal
      this.getGoodsCateList()
    },
    showAddCate () { // 点击添加分类按钮显示 添加分类会话框
      this.getParentCateList()
      this.addCateDialogVisible = true
    },
    async getParentCateList () { // 获取父级商品分类列表
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      console.log(res)
      if (res.meta.status !== 200) return this.$msg.error('获取分类失败！')
      this.parentCateList = res.data
    },
    selectCateKeys () { // 监听级联选择器改变事件 得到选中分类的id值
      if (this.selectKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectKeys[this.selectKeys.length - 1]
        this.addCateForm.cat_level = this.selectKeys.length
      } else if (this.selectKeys.length === 0) {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    addCateList () { // 点击确认按钮添加商品分类
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) return this.$msg.error('表单验证失败')
        const { data: res } = await this.$http.post('categories', this.addCateForm)
        console.log(res)
        if (res.meta.status !== 201) return this.$msg.error('添加分类失败！')
        this.$msg.success('添加分类成功')
        this.getGoodsCateList()
        this.addCateDialogVisible = false
      })
    },
    addCateDialogVisibleClosed () { // 添加分类会话框关闭，重置表单
      this.$refs.addCateFormRef.resetFields()
      this.selectKeys = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    }
  }
}
</script>

<style lang="less" scoped>
.treeTable {
  margin-top: 20px;
  font-size: 13px;

  .el-icon-success {
    color: green;
  }

  .el-icon-error {
    color: red;
  }
}
.el-cascader {
  width: 100%;
}

</style>
